function print(){
   echo -e "$2> $1 $3"
}
function print_error(){
   print "$1" '\e[31m' '\e[0m'
}
function print_success(){
   print "$1" '\e[34m' '\e[0m'
}

function mysql_exec(){
  msg=""
  if [ "$1" == "exec" ];then
  msg=`docker exec -it mysql mysql -uroot -p123456 --execute="$2"`
  else
  msg=`docker exec -i mysql mysql -uroot -p123456 $2 < "$c_dir/init_sql/$3"`
  fi
  if [ "$?" != "0" ];then
     print_error "\t执行脚本$3失败,脚本退出..."
     exit 1
  fi
  print "\t$mgs"
  print_success "\t执行脚本 $3成功！\n"
}

function pg_exec(){
  msg=""
  if [ "$1" == "exec" ];then
  msg=`docker exec -it postgres psql -U postgres -c "$2"`
  else
  cp_msg=`docker cp "$c_dir/init_sql/$3" postgres:/tmp/tmp.sql`
  print "\t$cp_mgs"
  msg=`docker exec -i postgres psql -U postgres -d $2 -f /tmp/tmp.sql`
  fi
  print "\t$mgs"
  if [ "$?" != "0" ];then
     print_error "\t执行脚本$3失败,脚本退出..."
     exit 1
  fi
  print_success "\t执行脚本 $3成功！\n"
}

c_dir=$(pwd)
echo "当前目录:$c_dir"
mysql_exec "exec" "create database if not exists zxwd_qm character set utf8" "初始化zxwd_qm数据库"
mysql_exec "sql" "zxwd_qm" "mysql.sql"
mysql_exec "sql" "zxwd_qm" "mysql_data.sql"
echo "初始化mysql脚本成功"

pg_exec "exec" "create database if not exists zxwd_qm" "初始化zxwd_qm数据库"
pg_exec "sql" "zxwd_qm" "pg.sql"
echo "初始化pg脚本成功"
